package com.abc.core.util;

import com.abc.core.def.Dna;
import com.abc.core.instance.bo.Inst;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;

import java.io.IOException;

public class InstDeserializer extends JsonDeserializer<Inst> {

    StaticDnaCacheService staticDnaCacheService;

    @Override
    public Inst deserialize(JsonParser jp, DeserializationContext arg1) throws IOException, JacksonException {
        JsonNode instNode = jp.getCodec().readTree(jp);
        String dnaCode = null;
        if (!instNode.get("dnaCode").isNull()) {
            dnaCode = instNode.get("dnaCode").asText();
        }

        String instType = null;
        if (!instNode.get("instType").isNull()) {
            instType = instNode.get("instType").asText();
        }

        //TODO   Dna dna = dnaCacheService.getStaticDnaCacheService().getDna(dnaCode);
        Dna dna = staticDnaCacheService.getDna(dnaCode);
        return CellTool.jsonNode2Inst(instNode, instType, dna);
    }

}
